package com.zkjd.lingdong

import android.app.Application
import android.content.Intent
import android.os.Build
import com.zkjd.lingdong.repository.DeviceRepository
import com.zkjd.lingdong.service.BleService
import dagger.hilt.android.HiltAndroidApp
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject

/**
 * 灵动贴应用入口类
 */
@HiltAndroidApp
class LingDongTieApp : Application() {
    
    @Inject
    lateinit var deviceRepository: DeviceRepository
    
    private val appScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
    
    override fun onCreate() {
        super.onCreate()
        
        // 初始化日志
        if (BuildConfig.DEBUG) {
            Timber.plant(Timber.DebugTree())
        }
        
        Timber.i("灵动贴应用启动，版本：${BuildConfig.VERSION_NAME}，设备：${Build.MANUFACTURER} ${Build.MODEL}")
        
        appScope.launch {
            // 启动蓝牙服务
            Timber.d("自动启动后台服务")
            startBleService()
        }
    }
    

    
    /**
     * 启动蓝牙服务
     */
    private fun startBleService() {
        try {
            val serviceIntent = Intent(this, BleService::class.java)
            
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                startForegroundService(serviceIntent)
            } else {
                startService(serviceIntent)
            }
            
            Timber.d("蓝牙服务已启动")
        } catch (e: Exception) {
            Timber.e(e, "启动蓝牙服务失败")
        }
    }
} 